package com.swm.utilAPI;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.swm.mapper.StockMapper;
import com.swm.pojo.Stock;
import com.swm.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.swm.utilAPI.ConditionUtilAPI.getDataFromAPI;
import static com.swm.utilAPI.ConditionUtilAPI.getPriceAPI;

/**
 * @author 师文明
 * @data 2024/12/23 下午3:11
 **/
public class getStockDayData471 {

    public static int allup;
    public static double allupnum;
    public static double sum;
    public static double sum3;
    public static int num0;
    public static int numu;
    public static int numd;
    public static int numu3;
    public static int numd3;

    public static DecimalFormat df = new DecimalFormat("#0.000");
    public static int ban = 0;
    static SqlSession sqlSession = SqlSessionUtil.openSession();
    static StockMapper mapper = sqlSession.getMapper(StockMapper.class);

    private static ArrayList<String> dateList = new ArrayList<String>();

    public static List<Stock> getStcokList() {
        List<Stock> stocks = mapper.selectAllStock();
        JSONObject jsonObject = new JSONObject();

        for (Stock stock : stocks) {
            String code = stock.getCode();
            int type = stock.getType();
            jsonObject.put(code, type);

        }
        return stocks;
    }

    public static void main(String[] args) throws Exception {


        ExecutorService executorService = Executors.newFixedThreadPool(50);
        //获取所有股票列表
        List<Stock> stcokList = getStcokList();
        for (Stock stock : stcokList) {
            executorService.execute(() -> {
                try {
                    getData(stock.getCode(), stock.getType(), stock.getName());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        }
        Thread.sleep(50000);
        double v0 = 0;
        if (allup != 0) {
            v0 = allupnum / allup;
        }

        System.out.println(("上涨次数" + numu + "  下跌次数" + numd));
        System.out.println(("平均涨幅：" + v0 + "上涨概率 " + df.format((double) numu / allup)));

        double v = 0;
        double v3 = 0;
        if (num0 != 0 && sum != 0) {
            v = sum / num0;
        }
        if (num0 != 0 && sum3 != 0) {
            v3 = sum3 / num0;
        }

        System.out.println("2-4");
        System.out.println(("一天后涨的次数：" + numu + "  跌的次数：" + numd + "  上涨概率： " + df.format((double) numu * 100 / num0)) + "%  平均涨幅：" + df.format(v));
        System.out.println(("3天后涨的次数：" + numu3 + "  跌的次数：" + numd3 + "  上涨概率： " + df.format((double) numu3 * 100 / num0)) + "%  平均涨幅：" + df.format(v3));
        System.out.println("板数：" + ban);

        System.out.println(dateList.size());
      /*dateList.forEach(date->{
           System.out.println(date);
       });*/


        // executorService.shutdown();
    }

    public static void getData(String code, int type, String name) throws Exception {

        JSONArray klines = getDataFromAPI(code, type);
        System.out.println(klines);
        if (klines != null && klines.size() > 150) {
            for (int i = 2; i < 102; i++) {
                //当天
                JSONObject jsonObject =JSONObject.parseObject(klines.get(klines.size() - i).toString()) ;
                //昨天
                JSONObject jsonObject1 =JSONObject.parseObject(klines.get(klines.size() - i-1).toString()) ;
                //昨日涨幅
                double v = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[9]);
                //开盘价
                double k = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[2]);
                //收盘价
                double s = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[3]);
                //最高价
                double g = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[5]);
                double v7 = (s - k) / k;
                boolean price = getPriceAPI(klines, i);
                //收盘与最高差距
                double v0000 = (g - s) * 100 / k;
                if (/*flag >1&&flag<4&&*/v7 > 0.03 && v < 9.8 && v > 0 && v0000 < 0.2 && price) {
                    //次日涨幅
                    double v11 = Double.parseDouble(klines.get(klines.size()-i+1).toString().split(",")[9]);

                    allupnum += v11;
                    allup++;
                    if (v11 > 0 || v11 == 0) {
                        numu++;
                    } else {
                        numd++;
                    }
                    sum += v11;
                    num0++;

                    if (!dateList.contains(klines.get(klines.size() - i).toString().split(",")[1])) {
                        dateList.add(klines.get(klines.size() - i).toString().split(",")[1]);
                    }
                    if (v11 > 9.8) {
                        ban++;
                    }
                    System.out.println(name + " : " + klines.get(klines.size() - i).toString().split(",")[1] + "  当日涨幅" + v + "   次日涨幅" + v11);
                }
            }
        }
    }

}

